package com.stm.bi.report;

import com.stm.base.api.RegionRemoteService;
import com.stm.base.api.WeatherRemoteService;
import com.stm.base.dto.query.DateRangeQry;
import com.stm.base.dto.query.RegionListQry;
import com.stm.base.dto.vo.RegionDetailVO;
import com.stm.base.dto.vo.RegionVO;
import com.stm.base.dto.vo.WeatherVO;
import com.stm.bi.core.util.DateUtils;
import com.stm.bi.dto.query.RegionalSaleQry;
import com.stm.bi.dto.vo.CityWeatherVO;
import com.stm.bi.dto.vo.RegionalWeatherVO;
import com.stm.bi.model.Chart;
import com.stm.bi.model.DateRange;
import com.stm.framework.dto.Response;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
import java.math.RoundingMode;

import static com.stm.bi.constant.PlanConstants.TYPE_WEEK;

/**
 * @author liuyx
 * @date 2025/4/3 14:36
 */
@Component
public class MacroeconomicDataQueryExe {
    @Resource
    private WeatherRemoteService weatherRemoteService;

    @Resource
    private RegionRemoteService regionRemoteService;

    private final List<Long> REGION_IDS = Arrays.asList(110100000000L, 120100000000L, 130100000000L, 140100000000L,
            150100000000L, 210100000000L, 220100000000L, 230100000000L, 310100000000L, 320100000000L, 330100000000L,
            340100000000L, 350100000000L, 360100000000L, 370100000000L, 410100000000L, 420100000000L, 430100000000L,
            440100000000L, 450100000000L, 460100000000L, 500100000000L, 510100000000L, 520100000000L, 530100000000L,
            540100000000L, 610100000000L, 620100000000L, 630100000000L, 640100000000L, 650100000000L, 710100000000L);


    /**
     * 宏观数据-温湿度
     **/
    public Response<RegionalWeatherVO> regionalTemperatureHumidity(RegionalSaleQry regionalSaleQry) {
        //根据日期范围和地区编码查询天气数据
        RegionalWeatherVO weatherVO = new RegionalWeatherVO();
        BigDecimal temperatureDifference = BigDecimal.ZERO;     //温差
        List<WeatherVO> weatherlist = new ArrayList<>();
        List<CityWeatherVO> cityWeatherVOS = new ArrayList<>();
        DateRangeQry qry = new DateRangeQry();
        LocalDate localDate = LocalDate.now();
        LocalDate startDate = localDate.withDayOfMonth(1);
        LocalDate lastDayOfMonth = localDate.withDayOfMonth(1).plusMonths(1).minusDays(1);
        qry.setStartDate(String.valueOf(startDate)).setEndDate(String.valueOf(lastDayOfMonth));
        //如果地区编码为空，则查询全国天气数据
        if (regionalSaleQry.getRegionCode() == null || regionalSaleQry.getRegionCode().toString().isEmpty()) {
            qry.setRegionId(0L);
            weatherVO.setRegionName("全国");
            //查询全国天气数据
            Response<List<WeatherVO>> weatherCollect = weatherRemoteService.selectByDateRange(qry);
            //过滤出省会城市天气数据
            weatherlist = weatherCollect.getData().stream()
                    .filter(weather -> weather.getRegionId() != null && weather.getRegionId().toString().endsWith("0100000000"))
                    .collect(Collectors.toList());

            //最高温
            BigDecimal maximumTemperature = getMaximumTemperature(weatherlist);
            //最低温
            BigDecimal minimumTemperature = getMinimumTemperature(weatherlist);
            //平均湿度
            BigDecimal humidity = calculateAverageHumidity(weatherlist);
            //温差
            temperatureDifference = maximumTemperature.subtract(minimumTemperature);

            weatherVO.setMaximumTemperature(maximumTemperature);
            weatherVO.setMinimumTemperature(minimumTemperature);
            weatherVO.setHumidity(humidity);
            weatherVO.setTemperatureDifference(temperatureDifference);

            // 计算省会城市的最大温度、最小温度、湿度的平均值和温差以及排名前5个温差最大的省会城市
            cityWeatherVOS = weatherlist.stream()
                    // 按regionId分组
                    .collect(Collectors.groupingBy(WeatherVO::getRegionId))
                    .entrySet().stream()  // 获取每个分组的entry（regionId和对应的天气数据）
                    .map(entry -> {
                        Long regionId = entry.getKey();
                        Response<RegionDetailVO> regionDetailVOResponse = regionRemoteService.selectById(regionId);
                        List<WeatherVO> regionWeatherList = entry.getValue();
                        //最高温
                        BigDecimal regionMaxTemperature = getMaximumTemperature(regionWeatherList);
                        //最低温
                        BigDecimal regionMinTemperature = getMinimumTemperature(regionWeatherList);
                        //平均湿度
                        BigDecimal avgHumidity = calculateAverageHumidity(regionWeatherList);
                        //温差
                        BigDecimal regionTemperatureDifference = regionMaxTemperature.subtract(regionMinTemperature);

                        // 创建CityWeatherVO对象并填充该regionId的统计数据
                        CityWeatherVO cityWeatherVO = new CityWeatherVO();
                        cityWeatherVO.setRegionId(regionId);
                        cityWeatherVO.setMaximumTemperature(regionMaxTemperature);
                        cityWeatherVO.setMinimumTemperature(regionMinTemperature);
                        cityWeatherVO.setHumidity(avgHumidity);
                        cityWeatherVO.setTemperatureDifference(regionTemperatureDifference);
                        if (regionId == 110100000000L) {
                            cityWeatherVO.setRegionName("北京市");
                        } else if (regionId == 120100000000L) {
                            cityWeatherVO.setRegionName("天津市");
                        } else if (regionId == 310100000000L) {
                            cityWeatherVO.setRegionName("上海市");
                        } else if (regionId == 500100000000L) {
                            cityWeatherVO.setRegionName("重庆市");
                        } else {
                            cityWeatherVO.setRegionName(regionDetailVOResponse.getData().getName());
                        }
                        return cityWeatherVO;
                    })
                    // 排序，按照温差降序排列
                    .sorted(Comparator.comparing(CityWeatherVO::getTemperatureDifference, Comparator.reverseOrder()))
                    // 限制返回前5个温差最大的城市
                    .limit(5)
                    .collect(Collectors.toList());


            weatherVO.setCityTableData(cityWeatherVOS);

        } else {//region不为空
            //查询地区名称
            Response<RegionDetailVO> regionDetailVOResponse = regionRemoteService.selectById(Long.valueOf(regionalSaleQry.getRegionCode()));
            if (regionDetailVOResponse.isSuccess() && regionDetailVOResponse.getData() != null) {
                weatherVO.setRegionName(regionDetailVOResponse.getData().getName());
            }
            //查询省下级城市的天气数据
            RegionListQry regionQry = new RegionListQry();
            regionQry.setParentId(Long.valueOf(regionalSaleQry.getRegionCode()));
            Response<List<RegionVO>> regionResponse = regionRemoteService.selectList(regionQry);
            // 初始化全省和省以下城市的温差数据
            BigDecimal provinceMaxTemperature = BigDecimal.ZERO;
            BigDecimal provinceMinTemperature = BigDecimal.ZERO;
            BigDecimal provinceTotalHumidity = BigDecimal.ZERO;
            int provinceHumidityCount = 0;
            if (regionResponse.isSuccess() || !regionResponse.getData().isEmpty()) {
                List<RegionVO> regions = regionResponse.getData();
                // 获取省下级城市的 RegionId 列表
                List<String> cityIds = regions.stream().map(RegionVO::getId).filter(Objects::nonNull).filter(id -> !id.equals(469000000000L) && !id.equals(500200000000L)).map(Object::toString).collect(Collectors.toList());
                // 查询这些城市的天气数据
                for (String cityId : cityIds) {
                    qry.setRegionId(Long.parseLong(cityId));

                    // 查询城市天气数据
                    Response<List<WeatherVO>> cityWeatherResponse = weatherRemoteService.selectByDateRange(qry);
                    Response<RegionDetailVO> regionDetailVOResponse1 = regionRemoteService.selectById(Long.valueOf(cityId));

                    if (cityWeatherResponse.isSuccess() && cityWeatherResponse.getData() != null) {
                        weatherlist = cityWeatherResponse.getData();

                        // 计算城市数据
                        //最高温
                        BigDecimal cityMaxTemperature = getMaximumTemperature(weatherlist);
                        //最低温
                        BigDecimal cityMinTemperature = getMinimumTemperature(weatherlist);

                        BigDecimal cityTotalHumidity = weatherlist.stream()
                                .filter(weather -> weather.getHumidity() != null)
                                .map(WeatherVO::getHumidity)
                                .reduce(BigDecimal::add)
                                .orElse(BigDecimal.ZERO);

                        long cityHumidityCount = weatherlist.stream()
                                .filter(weather -> weather.getHumidity() != null)
                                .count();

                        // 计算城市湿度平均值
                        BigDecimal cityAvgHumidity = cityHumidityCount > 0 ?
                                cityTotalHumidity.divide(new BigDecimal(cityHumidityCount), RoundingMode.HALF_UP) : BigDecimal.ZERO;

                        // 计算城市温差
                        BigDecimal cityTemperatureDifference = cityMaxTemperature.subtract(cityMinTemperature);

                        // 添加到温差前5的城市列表
                        CityWeatherVO cityWeatherVO = new CityWeatherVO();
                        cityWeatherVO.setRegionId(Long.valueOf(cityId));
                        cityWeatherVO.setMaximumTemperature(cityMaxTemperature);
                        cityWeatherVO.setMinimumTemperature(cityMinTemperature);
                        cityWeatherVO.setHumidity(cityAvgHumidity);
                        cityWeatherVO.setTemperatureDifference(cityTemperatureDifference);
                        if (cityId.equals("110100000000")) {
                            cityWeatherVO.setRegionName("北京市");
                        } else if (cityId.equals("120100000000")) {
                            cityWeatherVO.setRegionName("天津市");
                        } else if (cityId.equals("310100000000")) {
                            cityWeatherVO.setRegionName("上海市");
                        } else if (cityId.equals("500100000000")) {
                            cityWeatherVO.setRegionName("重庆市");
                        } else {
                            cityWeatherVO.setRegionName(regionDetailVOResponse1.getData().getName());
                        }
                        cityWeatherVOS.add(cityWeatherVO);

                        // 更新全省最大最小温度、湿度平均值和温差
                        provinceMaxTemperature = provinceMaxTemperature.max(cityMaxTemperature);
                        provinceMinTemperature = provinceMinTemperature.min(cityMinTemperature);
                        provinceTotalHumidity = provinceTotalHumidity.add(cityTotalHumidity);
                        provinceHumidityCount += cityHumidityCount;
                    }
                }
                // 计算全省湿度的平均值
                BigDecimal provinceAvgHumidity = provinceHumidityCount > 0 ?
                        provinceTotalHumidity.divide(new BigDecimal(provinceHumidityCount), RoundingMode.HALF_UP) : BigDecimal.ZERO;

                // 计算全省温差
                temperatureDifference = provinceMaxTemperature.subtract(provinceMinTemperature);


                // 排序省下级城市的温差并取前5名
                List<CityWeatherVO> top5Cities = cityWeatherVOS.stream()
                        .sorted((city1, city2) -> city2.getTemperatureDifference().compareTo(city1.getTemperatureDifference()))  // 根据温差降序排序
                        .limit(5)  // 取前5个温差最大的城市
                        .collect(Collectors.toList());

                weatherVO.setMaximumTemperature(provinceMaxTemperature);
                weatherVO.setMinimumTemperature(provinceMinTemperature);
                weatherVO.setHumidity(provinceAvgHumidity);
                weatherVO.setTemperatureDifference(temperatureDifference);
                weatherVO.setCityTableData(top5Cities);

            }

        }

        return Response.of(weatherVO);
    }

    /**
     * 最高温
     */
    private BigDecimal getMaximumTemperature(List<WeatherVO> weatherlist) {
        return weatherlist.stream()
                .filter(weather -> weather.getMaximumTemperature() != null)
                .map(WeatherVO::getMaximumTemperature)
                .max(BigDecimal::compareTo)
                .orElse(BigDecimal.ZERO);
    }

    /**
     * 最低温
     */
    private BigDecimal getMinimumTemperature(List<WeatherVO> weatherlist) {
        return weatherlist.stream()
                .filter(weather -> weather.getMinimumTemperature() != null)
                .map(WeatherVO::getMinimumTemperature)
                .min(BigDecimal::compareTo)
                .orElse(BigDecimal.ZERO);
    }

    /**
     * 平均湿度
     */
    private BigDecimal calculateAverageHumidity(List<WeatherVO> weatherlist) {
        BigDecimal totalHumidity = weatherlist.stream()
                .filter(weather -> weather.getHumidity() != null)
                .map(WeatherVO::getHumidity)
                .reduce(BigDecimal::add)
                .orElse(BigDecimal.ZERO);

        long nonNullCount = weatherlist.stream()
                .filter(weather -> weather.getHumidity() != null)
                .count();

        return nonNullCount > 0 ?
                totalHumidity.divide(new BigDecimal(nonNullCount), RoundingMode.HALF_UP) : BigDecimal.ZERO;
    }

    /**
     * 省全年温湿度走势图
     *
     * @param regionalSaleQry
     * @return
     */

    public Response<Chart> provinceTemperatureHumidity(RegionalSaleQry regionalSaleQry) {
        Chart chartVO = new Chart();
        RegionalWeatherVO weatherVO = new RegionalWeatherVO();
        List<String> items = chartVO.getItems();
        List<List<Object>> datas = new ArrayList<>();
        DateRangeQry qry = new DateRangeQry();
        Response<List<WeatherVO>> weatherCollect = Response.of(new ArrayList<>());
        LocalDate localDate = LocalDate.now();
        //当前日期所对应的年份
        String year = String.valueOf(localDate.getYear());
        //求出全年每个月度对应的周数，并求出每周的第一天和最后一天
        List<String> dateList = DateUtils.getDateList(year + "-" + "01-01", localDate.toString(), TYPE_WEEK);
        qry.setStartDate(year + "-" + "01-01").setEndDate(localDate.toString());
        if (regionalSaleQry.getType() == null || regionalSaleQry.getType().equals("temperature")) {
            items.add("最高温度");
            items.add("最低温度");
            items.add("温差");
        } else {
            items.add("湿度");
        }
        //如果地区编码为空，则查询全国天气数据
        String name = weatherVO.getRegionName();
        if (regionalSaleQry.getRegionCode() == null || regionalSaleQry.getRegionCode().toString().isEmpty()) {
            qry.setRegionId(0L);
            weatherVO.setRegionName("全国");
            //查询今年所有的天气数据
            weatherCollect = weatherRemoteService.selectByDateRange(qry.setRegionIds(REGION_IDS));

        } else {//具体省份
            //查询地区名称
            Response<RegionDetailVO> regionDetailVOResponse = regionRemoteService.selectById(Long.valueOf(regionalSaleQry.getRegionCode()));
            if (regionDetailVOResponse.isSuccess() && regionDetailVOResponse.getData() != null) {
                name = regionDetailVOResponse.getData().getName();
            }
            //查询省下级城市的天气数据
            RegionListQry regionQry = new RegionListQry();
            regionQry.setParentId(Long.valueOf(regionalSaleQry.getRegionCode()));
            Response<List<RegionVO>> regionResponse = regionRemoteService.selectList(regionQry);

            if (regionalSaleQry.getRegionCode().equals("820000000000")) {
                weatherVO.setRegionName("澳门特别行政区");
            } else if (regionalSaleQry.getRegionCode().equals("810000000000")) {
                weatherVO.setRegionName("香港特别行政区");
            }
            if (regionResponse.isSuccess() || !regionResponse.getData().isEmpty()) {
                List<RegionVO> regions = regionResponse.getData();

                if (!regions.isEmpty()) {
                    // 获取省下级城市的 RegionId 列表
                    List<Long> cityIds = regions.stream().map(RegionVO::getId).filter(Objects::nonNull)
                            .collect(Collectors.toList());
                    weatherCollect = weatherRemoteService.selectByDateRange(qry.setRegionIds(cityIds));
                }

            }
        }


        for (String date : dateList) {
            DateRange weekFirstAndEnd = DateUtils.getWeekFirstAndEnd(date);
            String dateName = "";
            dateName = Integer.parseInt(weekFirstAndEnd.getStartDate().substring(5, 7)) + "-" + Integer.parseInt(date.split("-")[1]);
            List<Object> dataList = new ArrayList<>();
            dataList.add(dateName);
            // 使用 stream 进行过滤
            if (weatherCollect.getData() != null && !weatherCollect.getData().isEmpty()) {
                List<WeatherVO> weatherlist = weatherCollect.getData().stream()
                        .filter(weather -> {
                            LocalDate weatherDate = LocalDate.parse(weather.getDate());
                            return !weatherDate.isBefore(LocalDate.parse(weekFirstAndEnd.getStartDate())) && !weatherDate.isAfter(LocalDate.parse(weekFirstAndEnd.getEndDate()));  // 过滤日期在 startDate 和 endDate 之间的元素
                        })
                        .collect(Collectors.toList());

                //type为温度
                if (regionalSaleQry.getType() == null || regionalSaleQry.getType().equals("temperature")) {

                    BigDecimal maximumTemperature = getMaximumTemperature(weatherlist);
                    BigDecimal minimumTemperature = getMinimumTemperature(weatherlist);
                    BigDecimal temperatureDifference = maximumTemperature.subtract(minimumTemperature);
                    dataList.add(maximumTemperature);
                    dataList.add(minimumTemperature);
                    dataList.add(temperatureDifference);

                }
                //type为湿度
                else if (regionalSaleQry.getType().equals("humidity")) {
                    BigDecimal humidity = calculateAverageHumidity(weatherlist);
                    dataList.add(humidity);
                }
            } else {
                dataList.add(null);
                dataList.add(null);
                dataList.add(null);
            }

            datas.add(dataList);
        }

        chartVO.setItems(items).setDatas(datas).setName(name);
        return Response.of(chartVO);
    }

    /**
     * 省会城市全年温湿度走势图
     *
     * @param regionalSaleQry
     * @return
     */
    public Response<Chart> capitalTemperatureHumidity(RegionalSaleQry regionalSaleQry) {
        Chart chartVO = new Chart();
        RegionalWeatherVO weatherVO = new RegionalWeatherVO();
        List<String> items = chartVO.getItems();
        List<List<Object>> datas = new ArrayList<>();
        DateRangeQry qry = new DateRangeQry();
        Response<List<WeatherVO>> weatherCollect = null;
        LocalDate localDate = LocalDate.now();
        //当前日期所对应的年份
        String year = String.valueOf(localDate.getYear());
        //求出全年每个月度对应的周数，并求出每周的第一天和最后一天
        List<String> dateList = DateUtils.getDateList(year + "-" + "01-01", localDate.toString(), TYPE_WEEK);
        qry.setStartDate(year + "-" + "01-01").setEndDate(localDate.toString());
        if (regionalSaleQry.getType() == null || regionalSaleQry.getType().equals("temperature")) {
            items.add("最高温度");
            items.add("最低温度");
            items.add("温差");
        } else {
            items.add("湿度");
        }
        //如果地区编码为空，则查询全国天气数据
        if (regionalSaleQry.getRegionCode() == null || regionalSaleQry.getRegionCode().isEmpty()) {
            qry.setRegionId(110100000000L);
            weatherVO.setRegionName("北京市");
            //查询今年所有的天气数据
            weatherCollect = weatherRemoteService.selectByDateRange(qry);

        } else if (regionalSaleQry.getRegionCode().equals("110000000000") || regionalSaleQry.getRegionCode().equals("120000000000")
                || regionalSaleQry.getRegionCode().equals("310000000000") || regionalSaleQry.getRegionCode().equals("500000000000")) {
            //直辖市
            chartVO.setItems(items).setDatas(datas).setName("");
            return Response.of(chartVO);

        } else {//具体省份
            //查询省下级城市的天气数据
            RegionListQry regionQry = new RegionListQry();
            regionQry.setParentId(Long.valueOf(regionalSaleQry.getRegionCode()));
            Response<List<RegionVO>> regionResponse = regionRemoteService.selectList(regionQry);
            if (regionResponse.getData() == null || regionResponse.getData().isEmpty()) {
                chartVO.setItems(items).setDatas(datas).setName("");
                return Response.of(chartVO);
            } else {
                List<RegionVO> regions = regionResponse.getData();
                // 获取省下级城市的 RegionId 列表
                List<Long> cityId = regions.stream().map(RegionVO::getId).filter(Objects::nonNull)
                        .filter(id -> id != null && id.toString().endsWith("0100000000"))
                        .collect(Collectors.toList());
                Response<RegionDetailVO> regionDetailVOResponse = regionRemoteService.selectById(Long.valueOf(cityId.get(0)));
                //查询地区名称
                if (regionDetailVOResponse.isSuccess() && regionDetailVOResponse.getData() != null) {
                    weatherVO.setRegionName(regionDetailVOResponse.getData().getName());
                }
                qry.setRegionId(cityId.get(0));
                weatherCollect = weatherRemoteService.selectByDateRange(qry.setRegionId(cityId.get(0)));

            }
        }


        for (String date : dateList) {
            DateRange weekFirstAndEnd = DateUtils.getWeekFirstAndEnd(date);
            // 使用 stream 进行过滤
            List<WeatherVO> weatherlist = weatherCollect.getData().stream()
                    .filter(weather -> {
                        LocalDate weatherDate = LocalDate.parse(weather.getDate());
                        return !weatherDate.isBefore(LocalDate.parse(weekFirstAndEnd.getStartDate())) && !weatherDate.isAfter(LocalDate.parse(weekFirstAndEnd.getEndDate()));  // 过滤日期在 startDate 和 endDate 之间的元素
                    })
                    .collect(Collectors.toList());
            String dateName = "";
            dateName = Integer.parseInt(weekFirstAndEnd.getStartDate().substring(5, 7)) + "-" + Integer.parseInt(date.split("-")[1]);
            List<Object> dataList = new ArrayList<>();
            dataList.add(dateName);
            //type为温度
            if (regionalSaleQry.getType() == null || regionalSaleQry.getType().equals("temperature")) {
                BigDecimal maximumTemperature = getMaximumTemperature(weatherlist);
                BigDecimal minimumTemperature = getMinimumTemperature(weatherlist);
                BigDecimal temperatureDifference = maximumTemperature.subtract(minimumTemperature);
                dataList.add(maximumTemperature);
                dataList.add(minimumTemperature);
                dataList.add(temperatureDifference);
            }
            //type为湿度
            else if (regionalSaleQry.getType().equals("humidity")) {
                BigDecimal humidity = calculateAverageHumidity(weatherlist);
                dataList.add(humidity);
            }
            datas.add(dataList);
        }

        chartVO.setItems(items).setDatas(datas).setName(weatherVO.getRegionName());
        return Response.of(chartVO);
    }

    /**
     * 温差分布图
     */
    public Response<Chart> regionalWeatherMap(RegionalSaleQry regionalSaleQry) {
        Chart chart = new Chart();
        BigDecimal temperatureDifference = BigDecimal.ZERO;
        List<String> items = chart.getItems();
        items.add("温差分布");
        List<List<Object>> datas = new ArrayList<>();
        // 获取所有省份
        RegionListQry regionListQry = new RegionListQry();
        regionListQry.setType(2);
        Response<List<RegionVO>> regionProvinceResponse = regionRemoteService.selectList(regionListQry);
        if (!regionProvinceResponse.isSuccess() || regionProvinceResponse.getData().isEmpty()) {
            return Response.of(chart);
        }
        //省份列表
        List<RegionVO> regionList = regionProvinceResponse.getData();
        DateRangeQry qry = new DateRangeQry();
        LocalDate localDate = LocalDate.now();
        LocalDate startDate = localDate.withDayOfMonth(1);
        LocalDate lastDayOfMonth = localDate.withDayOfMonth(1).plusMonths(1).minusDays(1);
        qry.setStartDate(String.valueOf(startDate)).setEndDate(String.valueOf(lastDayOfMonth));

        for (RegionVO region : regionList) {

            Long regionId = region.getId();
            String name = region.getName();
            if (region.getId().equals(820000000000L)) {
                name = "澳门特别行政区";
            } else if (region.getId().equals(810000000000L)) {
                name = "香港特别行政区";
            }
            //查询省的温差
            //查询省下级城市的天气数据
            List<Object> data = new ArrayList<>();
            RegionListQry regionQry = new RegionListQry();
            regionQry.setParentId(Long.valueOf(regionId));
            Response<List<RegionVO>> regionResponse = regionRemoteService.selectList(regionQry);
            if (regionResponse.getData().isEmpty() || !regionResponse.isSuccess()) {
                data.add(name);
                data.add(BigDecimal.ZERO);
                data.add(regionId);
                datas.add(data);
                continue;
            }
            if (regionResponse.isSuccess() || !regionResponse.getData().isEmpty()) {
                List<RegionVO> regions = regionResponse.getData();
                // 获取省下级城市的 RegionId 列表
                List<Long> cityIds = regions.stream().map(RegionVO::getId).filter(Objects::nonNull).collect(Collectors.toList());
                // 查询这些城市的天气数据
                qry.setRegionIds(cityIds);
                // 查询城市天气数据
                Response<List<WeatherVO>> cityWeatherResponse = weatherRemoteService.selectByDateRange(qry);

                if (cityWeatherResponse.isSuccess() && cityWeatherResponse.getData() != null) {
                    List<WeatherVO> weatherlist = cityWeatherResponse.getData();

                    // 计算城市数据
                    //最高温
                    BigDecimal cityMaxTemperature = getMaximumTemperature(weatherlist);
                    //最低温
                    BigDecimal cityMinTemperature = getMinimumTemperature(weatherlist);
                    // 更新全省最大最小温度
                    temperatureDifference = cityMaxTemperature.subtract(cityMinTemperature);
                }

            }
            data.add(name);
            data.add(temperatureDifference);
            data.add(regionId);
            datas.add(data);
        }
        // 阶梯分布区间
        chart.setName(30 + "," + 29 + "," + 20 + "," + 19 + "," + 10 + "," + 10);
        chart.setDatas(datas);
        return Response.of(chart);
    }
}
